package utils.export;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class XMLUtils {
	private static String path = "C:\\Users\\Gary Benattar\\workspace\\GTrend\\";
	private static Configuration config = HBaseConfiguration.create();

	public static boolean generateXMLFile() throws IOException
	{
		try{

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			//root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("data");

			Attr attrWikiUrl = doc.createAttribute("wiki-url");
			attrWikiUrl.setValue("");
			rootElement.setAttributeNode(attrWikiUrl);

			Attr attrWikiSection = doc.createAttribute("wiki-section");
			attrWikiSection.setValue("Gtrends Timeline on Toluna.com data");
			rootElement.setAttributeNode(attrWikiSection);

			doc.appendChild(rootElement);

			HTable trendsPerDate = new HTable(config, "trendsFinalCollocations");

			ResultScanner scanner = trendsPerDate.getScanner(new Scan());
			for (Result res = scanner.next(); res != null; res = scanner.next()) {
				for(KeyValue kv: res.list()){
					Element event = doc.createElement("event");

					Attr attrStartEvent = doc.createAttribute("start");
					attrStartEvent.setValue(Bytes.toString(kv.getRow()));
					event.setAttributeNode(attrStartEvent);

					Attr attrTitleEvent = doc.createAttribute("title");
					attrTitleEvent.setValue(Bytes.toString(kv.getQualifier()));
					event.setAttributeNode(attrTitleEvent);
					
					event.appendChild(doc.createTextNode(Bytes.toString(kv.getValue())));
					  
					rootElement.appendChild(event);
				}
			}
			trendsPerDate.close();
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result =  new StreamResult(new File(path + "\\WebContent\\XML\\timelineTrends.xml"));
			transformer.transform(source, result);

			System.out.println("XML FILE CREATED WITH SUCCESS");

		}catch(ParserConfigurationException pce){
			pce.printStackTrace();
		}catch(TransformerException tfe){
			tfe.printStackTrace();
		}
		return true;

	}
	
	public static void main(String[]a){
		// test XML
		try {
			generateXMLFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
